<?php
/**
 * 订单编辑时商品处理功能的实现方法
 * 对订单编辑的提交数据进行操作
 * @author chris.zhang
 * @package ome_order_edit
 * @copyright www.shopex.cn 2011.02.25
 *
 */
class ome_order_edit_goods{

    protected  $config = array(
        'app'       => 'ome',
        'html'      => 'admin/order/edit/goods_edit.html',
        'js_count'  => 'total_goods()',
        'is_add'    => true,
        'add_title' => '添加销售物料',
        'add_id'    => 'add_product',
    );

    /**
     * 获取商品类型页面配置
     * @return Araay conf
     */
    public function get_config(){
        return $this->config;
    }

    /**
     * 处理订单编辑时提交的数据
     * @access public
     * @param array $data 订单编辑的数据  //POST
     */
    public function process($data){
        if (!$data['goods']) return false;

        $obj        = $data['goods']['obj'];
        $num        = $data['goods']['num'];
        $price      = $data['goods']['price'];
        $obj_pmt_price  = $data['goods']['obj_pmt_price'];

        $new_num    = $data['goods']['newnum'];
        $new_price  = $data['goods']['newprice'];
        $new_obj_pmt_price  = $data['goods']['new_obj_pmt_price'];
        $order_id   = $data['order_id'];
        $shop_id   = $data['shop_id'];

        $oOrderItm  = app::get('ome')->model("order_items");
        $oOrderObj  = app::get('ome')->model("order_objects");

        $salesMLib = kernel::single('material_sales_material');
        $basicMStockLib = kernel::single('material_basic_material_stock');
        $salesMShopFreezeLib = kernel::single('material_sales_material_shop_freeze');

        $tmp_obj = array();
        $new_obj = array();

        $is_order_change = false;
        $is_goods_modify = false;

        $total = 0;
		$total_pmt_goods = 0;

        if ($obj && is_array($obj))
        foreach ($obj as $k => $v){
            $tmp_obj[$k] = array('obj_id'=>$k);
            foreach ($v as $n){
                //如果obj_id不存在，跳过
                $objInfo = $oOrderObj->dump($n);
                $itemInfo = $oOrderItm->dump(array('obj_id'=>$n));
                if (!$objInfo) continue;

                if (isset($num[$n])){
                    if ($data['do_action'] != 2){
                        if ($num[$n] < 1 || $num[$n] > 499999){
                            trigger_error('数量必须大于1且小于499999', E_USER_ERROR);
                        }
                    }

                    //优惠金额变化，那订单和商品就标记为变化
                    if ($objInfo['pmt_price'] != $obj_pmt_price[$n]){
                        $is_order_change = true;
                        $is_goods_modify = true;
                    }

                    if ($objInfo['quantity'] != $num[$n] || $objInfo['price'] != $price[$n]){
                        $t_n = $num[$n] - $objInfo['quantity'];
                        if ($objInfo['delete'] == 'true'){
                            $salesMShopFreezeLib->freeze($shop_id, $objInfo['goods_id'], $num[$n]);
                            $basicMStockLib->freeze($itemInfo['product_id'],$num[$n]);
                        }else {
                            if ($t_n < 0){
                                $salesMShopFreezeLib->unfreeze($shop_id, $objInfo['goods_id'], abs($t_n));
                                $basicMStockLib->unfreeze($itemInfo['product_id'],abs($t_n));
                            }elseif ($t_n > 0){
                                $salesMShopFreezeLib->freeze($shop_id, $objInfo['goods_id'], abs($t_n));
                                $basicMStockLib->freeze($itemInfo['product_id'],abs($t_n));
                            }
                        }
                        $is_order_change = true;
                        $is_goods_modify = true;
                    }else if ($objInfo['delete'] == 'true'){
                        $salesMShopFreezeLib->freeze($shop_id, $objInfo['goods_id'], $num[$n]);
                        $basicMStockLib->freeze($itemInfo['product_id'],$num[$n]);
                        $is_order_change = true;
                        $is_goods_modify = true;
                    }

                    $tmp_price = $price[$n]==0?0:$price[$n];
                    $pmt_price = $obj_pmt_price[$n]==0?0:$obj_pmt_price[$n];
                    $tmp_item = array (
                        'item_id'   => $itemInfo['item_id'],
                        'quantity'  => $num[$n],
                        'price'     => $tmp_price,
                        'pmt_price'     => $pmt_price,
                        'delete'    => 'false',
                    );
                    $tmp_item['amount'] = $tmp_item['quantity'] * $tmp_item['price'];
                    $tmp_item['sale_price'] = $tmp_item['amount'] - $pmt_price;

                    $total += $tmp_item['amount'];
                    $total_pmt_goods += $pmt_price;

                    $tmp_obj[$k]['amount'] = $tmp_item['amount'];
                    $tmp_obj[$k]['pmt_price'] = $pmt_price;
                    $tmp_obj[$k]['price'] = $tmp_item['price'];
                    $tmp_obj[$k]['quantity'] = $tmp_item['quantity'];
                    $tmp_obj[$k]['sale_price'] = $tmp_item['sale_price'];
                    $tmp_obj[$k]['delete'] = 'false';

                }else {
                    if ($objInfo['delete'] == 'false'){
                        $salesMShopFreezeLib->unfreeze($shop_id, $objInfo['goods_id'], $objInfo['quantity']);
                        $basicMStockLib->unfreeze($itemInfo['product_id'],$itemInfo['quantity']);
                        $is_order_change = true;
                        $is_goods_modify = true;
                    }
                    $tmp_item = array (
                        'item_id' => $itemInfo['item_id'],
                        'delete'  => 'true',
                    );

                    $tmp_obj[$k]['delete'] = 'true';
                }
                $tmp_obj[$k]['items'][$itemInfo['item_id']] = $tmp_item;
            }
        }

        //新增商品
        if ($new_num){
            $add_obj = array();
            foreach ($new_num as $key => $n){
                if ($data['do_action'] != 2){
                    if ($n < 1 || $n > 499999){
                        trigger_error('数量必须大于1且小于499999', E_USER_ERROR);
                    }
                }

                $tmp_price = $new_price[$key];
                $pmt_price = $new_obj_pmt_price[$key];
                $amount = $n * $tmp_price;
                $obj_number = $n;

                $salesMInfo = $salesMLib->getSalesMById($shop_id,$key);
                if($salesMInfo){
                    $basicMInfos = $salesMLib->getBasicMBySalesMId($salesMInfo['sm_id']);
                    if($basicMInfos){
                        if($salesMInfo['sales_material_type'] == 1){
                            foreach($basicMInfos as $kk => $basicMInfo){
                                $tmp['items'][0] = array(
                                    'order_id'   => $order_id,
                                    'product_id' => $basicMInfo['bm_id'],
                                    'bn' => $basicMInfo['material_bn'],
                                    'name' => $basicMInfo['material_name'],
                                    'price' => $tmp_price,
                                    'amount' => $amount,
                                    'sale_price'=> $amount - $pmt_price,
                                    'quantity' => $basicMInfo['number']*$obj_number,
                                    'sendnum' => 0,
                                    'item_type' => 'product'
                                );

                                $weight += $basicMInfo['weight']*$basicMInfo['number']*$obj_number;
                            }

                            $tmp['order_id']    = $order_id;
                            $tmp['obj_type']    = 'goods';
                            $tmp['obj_alias']   = '商品';
                            $tmp['goods_id']    = $salesMInfo['sm_id'];
                            $tmp['bn']          = $salesMInfo['sales_material_bn'];
                            $tmp['name']        = $salesMInfo['sales_material_name'];
                            $tmp['price']       = $tmp_price;
                            $tmp['quantity']    = $obj_number;
                            $tmp['amount']      = $amount;
                            $tmp['weight']      = $weight;
                            $tmp['pmt_price']   = $pmt_price;
                            $tmp['sale_price']  = $amount - $pmt_price;

                            $new_obj[] = $tmp;
                            $total += $tmp['amount'];
                            $total_pmt_goods += $pmt_price;
                            
                            //冻结
                            $salesMShopFreezeLib->freeze($shop_id, $tmp['goods_id'], $tmp['quantity']);
                            $basicMStockLib->freeze($tmp['items'][0]['product_id'],$tmp['items'][0]['quantity']);
                        }
                    }else{
                        continue;
                    }
                }else{
                    continue;
                }
            }
            $is_order_change = true;
            $is_goods_modify = true;
        }

        $rs = array(
            'oobj'  => $tmp_obj,
            'nobj'  => $new_obj,
            'total' => $total,
            'is_order_change' => $is_order_change,
            'is_goods_modify' => $is_goods_modify,
            'total_pmt_goods' => $total_pmt_goods,
        );

        return $rs;
    }

    /**
     * 判断这次提交的数据在处理完成后，是否还存在有正常的数据。
     * @param array $data 订单编辑的数据  //POST
     */
    public function is_null($data){
        if (!$data['goods']) return true;
        $obj        = $data['goods']['obj'];
        $num        = $data['goods']['num'];
        $price      = $data['goods']['price'];
        $new_num    = $data['goods']['newnum'];
        $new_price  = $data['goods']['newprice'];
        if ((empty($num) && empty($new_num)) || (empty($price) && empty($new_price))) return true;
        return false;
    }

    /**
     * 校验订单编辑时提交的数据
     * @param array $data 订单编辑的数据  //POST
     */
    public function valid($data){
        if (!$data['goods']) return true;

        $obj        = $data['goods']['obj'];
        $num        = $data['goods']['num'];
        $price      = $data['goods']['price'];
        $obj_pmt_price  = $data['goods']['obj_pmt_price'];

        $new_num    = $data['goods']['new_num'];
        $new_price  = $data['goods']['new_price'];
        $new_obj_pmt_price  = $data['goods']['new_obj_pmt_price'];

        if ($obj && is_array($obj))
        foreach ($obj as $k => $v){
            $tmp_item_sale_price = 0;
            foreach ($v as $n){
                if (isset($num[$n])){
                    if ($num[$n] < 1  || $num[$n] > 499999){
                        $rs = array(
                            'flag'  => false,
                            'msg'   => "商品数量必须大于1且小于499999",
                        );
                        return $rs;
                    }
                }
                $amount = $num[$n] * $price[$n];
                if ($obj_pmt_price[$n] < 0  || $obj_pmt_price[$n] > $amount){
                    $rs = array(
                        'flag' => false,
                        'msg' => "优惠金额必须大于等于0且小于等于销售金额",
                        'error_info' => array('name'=>'goods[obj_pmt_price]['.$n.']'),
                    );
                    return $rs;
                }
                $tmp_item_sale_price += $amount - $obj_pmt_price[$n];
            }

            if ($obj_pmt_price[$k] < 0 || $obj_pmt_price[$k] > $tmp_item_sale_price){
                $rs = array(
                    'flag' => false,
                    'msg' => "优惠金额必须大于等于0且小于等于销售金额",
                    'error_info' => array('name'=>'goods[obj_pmt_price]['.$k.']'),
                );
                return $rs;
            }

        }

        //普通商品
        if ($new_num)
        foreach ($new_num as $key => $n){
            if ($n < 1 || $n > 499999){
                $rs = array(
                    'flag'  => false,
                    'msg'   => "商品数量必须大于1且小于499999",
                );
                return $rs;
            }

            $amount = $n * $new_price[$key];
            if ($new_obj_pmt_price[$key] < 0  || $new_obj_pmt_price[$key] > $amount){
                $rs = array(
                    'flag' => false,
                    'msg' => "优惠金额必须大于等于0且小于等于销售金额",
                    'error_info' => array('name'=>'goods[new_obj_pmt_price]['.$key.']'),
                );
                return $rs;
            }

        }
        return true;
    }
}